﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using libs.Model;
using libs.DAL;
using libs.DBUtility;

namespace Sever
{
    /// <summary>
    /// UserHandler 的摘要说明
    /// </summary>
    public class UserHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string type = context.Request["type"];
            int qid = int.Parse(context.Request["id"]);
            userDao userd = new userDao();
            if (!userd.Exists(qid))
            {
                context.Response.Write("{\"result\":\"error\"}");
            }
            else
            {
                user uuu = userd.GetModel("id=" + qid);
                if (uuu.enable == false)
                {
                    context.Response.Write("{\"result\":\"error\"}");
                }
                else
                {
                    userDao dao = new userDao();
                    if (!string.IsNullOrEmpty(type))
                    {
                        switch (type)
                        {
                            //查询到所有用户
                            case "SearchAllUser":
                                List<user> AllUser = dao.GetList("");
                                string json = JsonHelper.SerializeObject(AllUser);
                                context.Response.Write("{\"result\":" + json + "}");
                                break;
                            //按id查询用户
                            case "SearchUserById":
                                string UserID = context.Request["id"];
                                user userId = dao.GetModel("id=" + UserID);
                                string jsonid = JsonHelper.SerializeObject(userId);
                                context.Response.Write("{\"result\":" + jsonid + "}");
                                break;
                            //删除用户
                            case "delete":
                                int del = int.Parse(context.Request["id"]);
                                bool redel = dao.Delete(del);
                                if (redel)
                                {
                                    context.Response.Write("{\"result\":\"ok\"}");
                                }
                                else
                                {
                                    context.Response.Write("{\"result\":\"fail\"}");
                                }
                                break;
                            //重置密码
                            case "resetPwd":
                                int resId = int.Parse(context.Request["id"]);
                                string resPwd = context.Request["pwd"];
                                bool readd = dao.resetPwd(resId, resPwd);
                                if (readd)
                                {
                                    context.Response.Write("{\"result\":\"ok\"}");
                                }
                                else
                                {
                                    context.Response.Write("{\"result\":\"fail\"}");
                                }
                                break;
                            //修改密码
                            case "updatePwd":
                                int updId = int.Parse(context.Request["id"]);
                                string newPwd = context.Request["newpwd"];
                                string oldPwd = context.Request["oldpwd"];
                                bool reupd = dao.updatePwd(updId, newPwd, oldPwd);
                                if (reupd)
                                {
                                    context.Response.Write("{\"result\":\"ok\"}");
                                }
                                else
                                {
                                    context.Response.Write("{\"result\":\"fail\"}");
                                }
                                break;
                            //禁用/启用
                            case "Enable":
                                int enId = int.Parse(context.Request["num"]);
                                user userEn = dao.GetModel("id=" + enId);
                                bool enab = dao.updateEnable(enId, !userEn.enable);
                                if (enab)
                                {
                                    context.Response.Write("{\"result\":\"ok\"}");
                                }
                                else
                                {
                                    context.Response.Write("{\"result\":\"fail\"}");
                                }
                                break;
                            //修改信息
                            case "update":
                                string upd = context.Request["model"];
                                user user = JsonHelper.DeserializeJsonToObject<user>(upd);
                                bool updMo = dao.Update(user);
                                if (updMo)
                                {
                                    context.Response.Write("{\"result\":\"ok\"}");
                                }
                                else
                                {
                                    context.Response.Write("{\"result\":\"fail\"}");
                                }
                                break;
                            //验证用户
                            case "UserInfo":
                                string uin = context.Request["model"];
                                user uinfo = JsonHelper.DeserializeJsonToObject<user>(uin);
                                user ueinfo = dao.GetModel("id=" + uinfo.id);
                                if (uinfo.enable != ueinfo.enable || uinfo.password != ueinfo.password)
                                {
                                    context.Response.Write("{\"result\":\"fail\"}");
                                }
                                else
                                {
                                    context.Response.Write("{\"result\":\"ok\"}");
                                }
                                break;
                            //添加用户
                            case "AddUser":
                                string add = context.Request["model"];
                                user addu = JsonHelper.DeserializeJsonToObject<user>(add);
                                bool res = dao.Add(addu);
                                if (res)
                                {
                                    context.Response.Write("{\"result\":\"ok\"}");
                                }
                                else
                                {
                                    context.Response.Write("{\"result\":\"fail\"}");
                                }
                                break;
                        }
                    }
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}